package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.vo.FamilyMemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.xml.validation.Validator;
import java.util.Arrays;

/**
 * 家属和老人关联Service业务层处理
 *
 * @author ly
 * @date 2025-10-24
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService {
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询家属和老人关联
     *
     * @param id 家属和老人关联主键
     * @return 家属和老人关联
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id) {
        return getById(id);
    }

    /**
     * 查询家属和老人关联列表
     *
     * @param familyMemberElder 家属和老人关联
     * @return 家属和老人关联
     */
    @Override
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder) {
        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
    }

    /**
     * 新增家属和老人关联
     *
     * @param familyMemberElder 家属和老人关联
     * @return 结果
     */
    @Override
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return save(familyMemberElder) ? 1 : 0;
    }

    /**
     * 修改家属和老人关联
     *
     * @param familyMemberElder 家属和老人关联
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return updateById(familyMemberElder) ? 1 : 0;
    }

    /**
     * 批量删除家属和老人关联
     *
     * @param ids 需要删除的家属和老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除家属和老人关联信息
     *
     * @param id 家属和老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<FamilyMemberVO> watchfamily(Integer pageNum, Integer pageSize) {
        Long userId = UserThreadLocal.getUserId();
        PageHelper.startPage(pageNum, pageSize);
        List<FamilyMemberElder> familyMemberElderList = familyMemberElderMapper.watchfamily(userId);
        List<FamilyMemberVO> list = new ArrayList<>();
        familyMemberElderList.forEach(familyMemberElder -> {
            FamilyMemberVO familyMemberVO = new FamilyMemberVO();
            familyMemberVO.setMid(String.valueOf(familyMemberElder.getMemberId()));
            familyMemberVO.setMremark(familyMemberElder.getRemark());
            familyMemberVO.setElderId(String.valueOf(familyMemberElder.getElderId()));
            Elder elder = elderMapper.selectById(familyMemberElder.getElderId());
            familyMemberVO.setName(elder.getName());
            familyMemberVO.setImage(elder.getImage());
            familyMemberVO.setBedNumber(elder.getBedNumber());
            Bed bed = bedMapper.selectById(elder.getBedId());
            Room room = roomMapper.selectById(bed.getRoomId());
            familyMemberVO.setTypeName(room.getTypeName());
            LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Device::getLocationType, 0);
            queryWrapper.eq(Device::getRemark, elder.getName());
            List<Device> devices = deviceMapper.selectList(queryWrapper);
            devices.forEach(device -> {
                familyMemberVO.setIotId(device.getIotId());
                familyMemberVO.setDeviceName(device.getDeviceName());
                familyMemberVO.setProductKey(device.getProductKey());
            });
            list.add(familyMemberVO);
        });
        return list;

    }

}
